package com.hqj.demo.controller;

import com.hqj.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tc")
public class Test01Controller {

    @RequestMapping("/t01")
    @ResponseBody   //说明是-把数据返回到页面
    public String test01(){
        return "123";
    }

    @RequestMapping("/index")
    public String index(){
        return "/index";
    }

    @RequestMapping("/t02")
    //满足页面的调整与数据的传递 modelAndView
    public ModelAndView jumpPageAndValue(){
        ModelAndView mav = new ModelAndView();
        //设置键值对，把值传递到跳转的页面
        mav.addObject("value","test数据");

        //设置跳转的页面
        mav.setViewName("/index");

        return mav;
    }

    @RequestMapping("/t03")
    public String requestJump(HttpServletRequest req){
        //通过设置请求属性的方式传值到跳转的页面
        req.setAttribute("req","request传值的数据");
        return "/index";

    }

    @RequestMapping("/t04")
    @ResponseBody
    public User getUser(){
        User u = new User("hqj001","男",29,new Date());
        return u;
    }

    @RequestMapping("/t06")
    @ResponseBody
    public List<User> findUsers(){
        List<User> list = new ArrayList<>();
        User u = new User("hqj001","男",29,new Date());
        User u1 = new User("hqj001","男",29,new Date());

        list.add(u);
        list.add(u1);
        return list;
    }

    @RequestMapping("/t05")
    public String requestJumpObject(HttpServletRequest req){
        User u = new User("hqj001","男",29,new Date());
        //通过设置请求属性的方式传值到跳转的页面
        req.setAttribute("user",u);
        return "/index";

    }

    @RequestMapping("/t07")
    public String requestJumpList(HttpServletRequest req){
        List<User> list = new ArrayList<>();
        User u = new User("hqj001","男",29,new Date());
        User u1 = new User("hqj001","男",29,new Date());

        list.add(u);
        list.add(u1);
        //通过设置请求属性的方式传值到跳转的页面
        req.setAttribute("list",list);
        return "/index";

    }
}
